
import { Body, Controller, Get,Post } from '@nestjs/common';
import { CategoryService } from 'src/services/category/category.service'
import { ICategory } from 'src/types/category.interface';
import HttpResponse from 'src/types/http.response.interface';

type UnionType = ICategory[] | ICategory;


@Controller('categories')
export class CategoryController {
      //等价于上面的写法
      constructor(private readonly  categoryService:CategoryService){

      }
      @Get()  
      async findAll():Promise<HttpResponse<ICategory[]>>{
         const data = await this.categoryService.findService();
         if(Array.isArray(data)){
          return {
              success:true,
              message:"查询数据成功",
              data,
          }
         }
         else{
          return {
              success:false,
              message:"查询数据失败",
              data:[],
          }
         }
         
      }
      /**
     * 用来通过postman或者insomnia来导入数据
     * 在浏览器中输入http://localhost:9898/categories/importData
     */
    @Post('/importData')
    //DTO Data Transfer Object 数据传输对象，当客户端把数据传送到后台的
    async create(@Body() requestParamsBody:UnionType) {

        const data:UnionType = await this.categoryService.importData(requestParamsBody);
        return {
            success:true,
            message:"数据导入成功",
            data
        };
    }

    
  
}


